const db = require('../db/index')
const connection = require('../db/index')

//添加用户信息
exports.addUser = (req,res)=>{
  const userMessage = req.body
  const sql = `insert into users set ?`
  db.query(sql, { 
    name:userMessage.name,
    sex:userMessage.sex,
    url:userMessage.url,
    class:userMessage.class,
    accountNum:userMessage.accountNum,
    password:userMessage.password,
    question1:userMessage.question[0],
    question2:userMessage.question[1],
    question3:userMessage.question[2],
    answer1:userMessage.answer[0],
    answer2:userMessage.answer[1],
    answer3:userMessage.answer[2],
  }, function
  (err, results) {
  // 执行 SQL 语句失败
  if (err) return res.json({ status: 1, message: err.message })
  // 注册成功
  res.json({ code:200, message: '上传成功！' })
  })
}

//登录
exports.userLogin = (req,res)=>{
  const userMessage = req.body
  const sql = `select * from users where accountNum = ?`
  db.query(sql,userMessage.accountNum
  , function
  (err, results) {
  // 执行 SQL 语句失败
  if (err) return res.json({ status: 1, message: err.message })
    var data = JSON.parse(JSON.stringify(results))
    console.log(data,'data')
    if(data[0] == null){
      res.json({ code:201, message: '未查询到账号' })
    }else{
      results.forEach((row) => {
        if(userMessage.passWord == row.password){
          res.json({ code:200, data:results[0], message: '登录成功！' })
        }else{
          res.json({ code:500, message: '登录失败' })
        }
      });
    }
  })
}

//查询用户信息
exports.getUserMessage = (req,res)=>{
  const id = req.body.id
  const sql = `select * from users where id = ?`
  db.query(sql,id
  , function
  (err, results) {
  // 执行 SQL 语句失败
  if (err) return res.json({ status: 1, message: err.message })
    res.json({
      code:200,
      message:'查找成功',
      data:results
  })
  })
}

//更新用户信息
exports.updateUserMessage = (req,res)=>{
  const id = req.body.id
  const data = { 
    name:req.body.name,
    sex:req.body.sex,
    url:req.body.url,
    class:req.body.class,
    accountNum:req.body.accountNum,
    password:req.body.password,
    question1:req.body.question[0],
    question2:req.body.question[1],
    question3:req.body.question[2],
    answer1:req.body.answer[0],
    answer2:req.body.answer[1],
    answer3:req.body.answer[2],
  }
  console.log(data)
  const sql = `update users  set ?  where id = ?;`
  db.query(sql,[data,id]
  , function
  (err, results) {
  // 执行 SQL 语句失败
  if (err) return res.json({ status: 1, message: err.message })
    res.json({
      code:200,
      message:'更新成功',
  })
  })
}


//获取用户列表
exports.getMemberList = (req,res)=>{
  const pageNum = req.body.pageNum
  const pageSize = req.body.pageSize
  if(req.body.keyword == ''){
    const sql = `select * from users LIMIT `+(pageNum-1)*pageSize+`,`+pageSize+`;SELECT COUNT(*) as total FROM users;`
    connection.query(sql,function
      (err, results) {
      res.json({
        code:200,
        message:'查找成功',
        data:results[0],
        total:JSON.parse(JSON.stringify(results[1]))[0].total
      })
      })
  }else if(req.body.keyword != ''){
    const keyword = "%"+req.body.keyword+"%"
    const sql = `select * from users where name like ? or class like ?;SELECT COUNT(*) as total FROM users where name like ? or class like ?`
    connection.query(sql,[keyword,keyword,keyword,keyword,keyword,keyword],function
      (err, results) {
      // 执行 SQL 语句失败
      if (err) return res.json({ status: 1, message: err.message })
        res.json({
          code:200,
          message:'查找成功',
          data:results[0],
          total:JSON.parse(JSON.stringify(results[1]))[0].total
        })
      })
  }
}